﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using HYSoft.IDAL;
using HYSoft.DBUtility;//Please add references
namespace HYSoft.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:bpinfo
	/// </summary>
	public partial class bpinfo:Ibpinfo
	{
		public bpinfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ljbp_sec_id", "tb_bpinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ljbp_sec_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tb_bpinfo");
			strSql.Append(" where ljbp_sec_id=@ljbp_sec_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@ljbp_sec_id", SqlDbType.Int,4)			};
			parameters[0].Value = ljbp_sec_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(HYSoft.Model.bpinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tb_bpinfo(");
			strSql.Append("route_name,slop_constr_site,sec_location,ljbp_sec_id,bpgd,bppd,ljhdmxs,ljlx,ljkd,ljdzlx,ljfhgclx,ljfhgccl)");
			strSql.Append(" values (");
			strSql.Append("@route_name,@slop_constr_site,@sec_location,@ljbp_sec_id,@bpgd,@bppd,@ljhdmxs,@ljlx,@ljkd,@ljdzlx,@ljfhgclx,@ljfhgccl)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@route_name", SqlDbType.Char,255),
					new SqlParameter("@slop_constr_site", SqlDbType.Char,255),
					new SqlParameter("@sec_location", SqlDbType.Char,255),
					new SqlParameter("@ljbp_sec_id", SqlDbType.Int,4),
					new SqlParameter("@bpgd", SqlDbType.Int,4),
					new SqlParameter("@bppd", SqlDbType.Int,4),
					new SqlParameter("@ljhdmxs", SqlDbType.Char,50),
					new SqlParameter("@ljlx", SqlDbType.Char,50),
					new SqlParameter("@ljkd", SqlDbType.Int,4),
					new SqlParameter("@ljdzlx", SqlDbType.Char,50),
					new SqlParameter("@ljfhgclx", SqlDbType.Char,50),
					new SqlParameter("@ljfhgccl", SqlDbType.Char,50)};
			parameters[0].Value = model.route_name;
			parameters[1].Value = model.slop_constr_site;
			parameters[2].Value = model.sec_location;
			parameters[3].Value = model.ljbp_sec_id;
			parameters[4].Value = model.bpgd;
			parameters[5].Value = model.bppd;
			parameters[6].Value = model.ljhdmxs;
			parameters[7].Value = model.ljlx;
			parameters[8].Value = model.ljkd;
			parameters[9].Value = model.ljdzlx;
			parameters[10].Value = model.ljfhgclx;
			parameters[11].Value = model.ljfhgccl;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HYSoft.Model.bpinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_bpinfo set ");
			strSql.Append("route_name=@route_name,");
			strSql.Append("slop_constr_site=@slop_constr_site,");
			strSql.Append("sec_location=@sec_location,");
			strSql.Append("bpgd=@bpgd,");
			strSql.Append("bppd=@bppd,");
			strSql.Append("ljhdmxs=@ljhdmxs,");
			strSql.Append("ljlx=@ljlx,");
			strSql.Append("ljkd=@ljkd,");
			strSql.Append("ljdzlx=@ljdzlx,");
			strSql.Append("ljfhgclx=@ljfhgclx,");
			strSql.Append("ljfhgccl=@ljfhgccl");
			strSql.Append(" where bpid=@bpid");
			SqlParameter[] parameters = {
					new SqlParameter("@route_name", SqlDbType.Char,255),
					new SqlParameter("@slop_constr_site", SqlDbType.Char,255),
					new SqlParameter("@sec_location", SqlDbType.Char,255),
					new SqlParameter("@bpgd", SqlDbType.Int,4),
					new SqlParameter("@bppd", SqlDbType.Int,4),
					new SqlParameter("@ljhdmxs", SqlDbType.Char,50),
					new SqlParameter("@ljlx", SqlDbType.Char,50),
					new SqlParameter("@ljkd", SqlDbType.Int,4),
					new SqlParameter("@ljdzlx", SqlDbType.Char,50),
					new SqlParameter("@ljfhgclx", SqlDbType.Char,50),
					new SqlParameter("@ljfhgccl", SqlDbType.Char,50),
					new SqlParameter("@bpid", SqlDbType.Int,4),
					new SqlParameter("@ljbp_sec_id", SqlDbType.Int,4)};
			parameters[0].Value = model.route_name;
			parameters[1].Value = model.slop_constr_site;
			parameters[2].Value = model.sec_location;
			parameters[3].Value = model.bpgd;
			parameters[4].Value = model.bppd;
			parameters[5].Value = model.ljhdmxs;
			parameters[6].Value = model.ljlx;
			parameters[7].Value = model.ljkd;
			parameters[8].Value = model.ljdzlx;
			parameters[9].Value = model.ljfhgclx;
			parameters[10].Value = model.ljfhgccl;
			parameters[11].Value = model.bpid;
			parameters[12].Value = model.ljbp_sec_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int bpid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_bpinfo ");
			strSql.Append(" where bpid=@bpid");
			SqlParameter[] parameters = {
					new SqlParameter("@bpid", SqlDbType.Int,4)
			};
			parameters[0].Value = bpid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteBySecId(int ljbp_sec_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_bpinfo ");
			strSql.Append(" where ljbp_sec_id=@ljbp_sec_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@ljbp_sec_id", SqlDbType.Int,4)			};
			parameters[0].Value = ljbp_sec_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string bpidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_bpinfo ");
			strSql.Append(" where bpid in ("+bpidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.bpinfo GetModel(int bpid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 bpid,route_name,slop_constr_site,sec_location,ljbp_sec_id,bpgd,bppd,ljhdmxs,ljlx,ljkd,ljdzlx,ljfhgclx,ljfhgccl from tb_bpinfo ");
			strSql.Append(" where bpid=@bpid");
			SqlParameter[] parameters = {
					new SqlParameter("@bpid", SqlDbType.Int,4)
			};
			parameters[0].Value = bpid;

			HYSoft.Model.bpinfo model=new HYSoft.Model.bpinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.bpinfo DataRowToModel(DataRow row)
		{
			HYSoft.Model.bpinfo model=new HYSoft.Model.bpinfo();
			if (row != null)
			{
				if(row["bpid"]!=null && row["bpid"].ToString()!="")
				{
					model.bpid=int.Parse(row["bpid"].ToString());
				}
				if(row["route_name"]!=null)
				{
					model.route_name=row["route_name"].ToString();
				}
				if(row["slop_constr_site"]!=null)
				{
					model.slop_constr_site=row["slop_constr_site"].ToString();
				}
				if(row["sec_location"]!=null)
				{
					model.sec_location=row["sec_location"].ToString();
				}
				if(row["ljbp_sec_id"]!=null && row["ljbp_sec_id"].ToString()!="")
				{
					model.ljbp_sec_id=int.Parse(row["ljbp_sec_id"].ToString());
				}
				if(row["bpgd"]!=null && row["bpgd"].ToString()!="")
				{
					model.bpgd=int.Parse(row["bpgd"].ToString());
				}
				if(row["bppd"]!=null && row["bppd"].ToString()!="")
				{
					model.bppd=int.Parse(row["bppd"].ToString());
				}
				if(row["ljhdmxs"]!=null)
				{
					model.ljhdmxs=row["ljhdmxs"].ToString();
				}
				if(row["ljlx"]!=null)
				{
					model.ljlx=row["ljlx"].ToString();
				}
				if(row["ljkd"]!=null && row["ljkd"].ToString()!="")
				{
					model.ljkd=int.Parse(row["ljkd"].ToString());
				}
				if(row["ljdzlx"]!=null)
				{
					model.ljdzlx=row["ljdzlx"].ToString();
				}
				if(row["ljfhgclx"]!=null)
				{
					model.ljfhgclx=row["ljfhgclx"].ToString();
				}
				if(row["ljfhgccl"]!=null)
				{
					model.ljfhgccl=row["ljfhgccl"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select bpid,route_name,slop_constr_site,sec_location,ljbp_sec_id,bpgd,bppd,ljhdmxs,ljlx,ljkd,ljdzlx,ljfhgclx,ljfhgccl ");
			strSql.Append(" FROM tb_bpinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" bpid,route_name,slop_constr_site,sec_location,ljbp_sec_id,bpgd,bppd,ljhdmxs,ljlx,ljkd,ljdzlx,ljfhgclx,ljfhgccl ");
			strSql.Append(" FROM tb_bpinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_bpinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.bpid desc");
			}
			strSql.Append(")AS Row, T.*  from tb_bpinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tb_bpinfo";
			parameters[1].Value = "bpid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

